package cn.lizemin.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lzm
 * @Date 2024/7/27
 * @description 注意事项：每个servlet有自己独立的servlet配置，彼此之间互不影响
 * 这个servletConfig是Tomcat解析web.xml后，自动调用每个servlet的init(ServletConfig config)方法传进来的
 * 类似于spring的aware接口
 */
@WebServlet(urlPatterns = "/servlet3")
public class MyServlet3 extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取项目中某个目录的绝对路径
        ServletContext servletContext = getServletContext();
        String path = servletContext.getRealPath("upload");
        System.out.println(path);

        // 获取项目的contextPath, 其实也就是springMVC中的contextPath，相当于项目的API统一前缀
        String contextPath = servletContext.getContextPath();
        System.out.println(contextPath);

        servletContext.setAttribute("company", "oracle");
        super.service(req, resp);
    }


}
